package com.project.ssm.dao;

import com.project.ssm.domain.Role;
import com.project.ssm.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IUserDao {
    @Select("select * from users where username=#{username}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "email", column = "email"),
            @Result(property = "phoneNum", column = "phoneNum"),
            @Result(property = "userStatus", column = "userStatus"),
            @Result(property = "roles", column = "id", javaType = java.util.List.class, many = @Many(select = "com.project.ssm.dao.IRoleDao.findRoleByUserId"))
    })
    public UserInfo findByUsername(String username) throws Exception;

    @Select("select * from users")
    public List<UserInfo> findAll() throws Exception;

    @Insert("insert into users values(NULL, #{email}, #{username}, #{password}, #{phoneNum}, #{userStatus})")
    public void save(UserInfo userInfo);

    @Select("select * from users where id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "email", column = "email"),
            @Result(property = "phoneNum", column = "phoneNum"),
            @Result(property = "userStatus", column = "userStatus"),
            @Result(property = "roles", column = "id", javaType = java.util.List.class, many = @Many(select = "com.project.ssm.dao.IRoleDao.findRoleByUserId"))
    })
    public UserInfo findById(Integer id);

    @Select("select * from role where id not in (select roleId from user_role where userId = #{id})")
    public List<Role> findOtherRole(Integer id) throws Exception;

    @Insert("insert into user_role values(#{userId}, #{roleId})")
    public void addRoleToUser(@Param("userId") Integer userId, @Param("roleId") Integer roleId)throws Exception;
}
